/*
 * Application\Inc\message.h
 *
 * Cargo message handler
 *
 * Author: Chao-Chi Kuo <chaochi_kuo@systech.com.tw>
 * Copyright (c) 2010 SYSTEMS & TECHNOLOGY CORP. <http://www.systech.com.tw/>
 * All rights reserved.
 *
 * Modifications:
 * 2009/12/8 Chao-Chi Kuo	first implemetation
 *
 * $Id: message.h 250 2010-11-04 07:08:28Z joseph_hsu $
 */
#ifndef __MESSAGE_H_
#define __MESSAGE_H_

#ifdef  __cplusplus
extern "C" {
#endif		/* __cplusplus */

#include "command_data.h"
#include "type.h"

/* input/output port */
#define CONSOLE_PORT	0
#define GPRS_PORT		1
#define SMS_PORT		2

/* response mode */
#define SEMICOLON		0
#define ENTER			1

#define SIGNATURE		0xB164B054
#define PRODUCT_ID		0x31
#define MSG_PROTOCOL	0x10

/* msg_type */
#define AT_CMD_REQUEST	0x01
#define AT_CMD_RESPONSE	0x02
#define ACKNOWLEDGE		0x03
#define ASYNC_TEXT_DATA	0x04
#define ASYNC_POS_DATA	0x05
#define HEART_BEAT_DATA	0x06
#define OTA_CMD			0x07

/* message id */
#define GETPDS			0x0000
#define GETLOG			0x0001
#define PDSR			0x0002
#define GPS				0x0003
#define POWER_ON		0x0004
#define LOC				0x0005
#define SOS				0x000D
#define POWER_OFF		0x0016
#define GETLOG_IDX1		0x0021
#define GETLOG_IDX2		0x0022
#define GETLOG_IDX3		0x0023
#define GETLOG_IDX4		0x0024
#define GETLOG_DST		0x0025
#define GF_ENTRY		0x00A4
#define GF_EXIT			0x00A5
#define BATTERY_LOW		0x00A8
#define HEART_BEAT		0x00AB
#define SGF				0x00AE
#define MAN_DOWN		0x00B4

/* sms message type */
#define STANDARD		0x01
#define TEXT_SMS		0x02
#define GOOGLE_MAP		0x04
#define URL				0x08
#define GARMIN			0x10

/* Message Header Struct  */
typedef struct _MSG_HEADER {
	unsigned long		signature;
	unsigned char		data_packet;
	unsigned short		tran_id;
	unsigned char		modem_id_len;
	unsigned long long	modem_id;
	unsigned char		product_id;
	unsigned char		msg_protocol;
	unsigned char		msg_type;
	unsigned short		msg_id;
	unsigned short		data_len;
}MSG_HEADER;

/* Message Foremat- Postion Data */
typedef struct _MSG_POSITION {
	MSG_HEADER			msg_header;
	unsigned char		rtc_hour;
	unsigned char		rtc_min;
	unsigned char		rtc_sec;
	unsigned char		rtc_year;
	unsigned char		rtc_month;
	unsigned char		rtc_day;
	unsigned char		gps_hour;
	unsigned char		gps_min;
	unsigned char		gps_sec;
	unsigned char		gps_year;
	unsigned char		gps_month;
	unsigned char		gps_day;
	signed long			latitude;
	signed long			longitude;
	signed long			altitude;
	unsigned short		speed;
	unsigned short		direction;
	unsigned long		odometer;
	unsigned short		hdop;
	unsigned char		sat_num;
	unsigned char		input;
	unsigned char		output;
	unsigned char		veh_status;
	unsigned short		analog_in1;
	unsigned short		analog_in2;
	unsigned char		send_hour;
	unsigned char		send_min;
	unsigned char		send_sec;
	unsigned char		send_year;
	unsigned char		send_month;
	unsigned char		send_day;
	unsigned char		check_sum;
}MSG_POSITION;

/* Message Foremat- Acknowledgemnet */
typedef struct _MSG_ACK {
	MSG_HEADER			msg_header;
	unsigned short		status;
	unsigned char		check_sum;
}MSG_ACK;

/* Message Foremat- HeartBeat */
typedef struct _MSG_HB {
	MSG_HEADER			msg_header;
	unsigned char		rtc_hour;
	unsigned char		rtc_min;
	unsigned char		rtc_sec;
	unsigned char		rtc_year;
	unsigned char		rtc_month;
	unsigned char		rtc_day;
	unsigned char		check_sum;
}MSG_HB;


typedef struct _LOG_MSG {
	MSG_POSITION	msg;
	unsigned char	sms_idx;
	unsigned char	sms_type;
	unsigned char	gf_idx;
	char			rsvd[84 - sizeof(MSG_POSITION) - 3];
}LOG_MSG;

#define MSG_HEADER_LEN		sizeof(MSG_HEADER)
#define MSG_POSITION_LEN	sizeof(MSG_POSITION)
#define MSG_HB_LEN			sizeof(MSG_HB)
#define LOG_MSG_LEN			sizeof(LOG_MSG)

/*******************************************************/

/* OLD Format */
typedef struct _MSG_OLD_HEADER {
	unsigned short tran_id;
	unsigned char msg_encoding;
	unsigned char msg_type;
}MSG_OLD_HEADER;

typedef struct _MSG_OLD_HEADER_2 {
	var16 tran_id;
	unsigned char msg_encoding;
	unsigned char msg_type;
	var16 data_len;
}MSG_OLD_HEADER_2;


/* Message Format - Position Data */
typedef struct _MSG_OLD_POSITION {
	MSG_OLD_HEADER		msg_header;
	unsigned long long	modem_id;
	unsigned short		msg_id;
	var16				data_len;
	unsigned char		gps_hour;
	unsigned char		gps_min;
	unsigned char		gps_sec;
	unsigned char		gps_year;
	unsigned char		gps_month;
	unsigned char		gps_day;
	unsigned long		latitude;
	unsigned long		longitude;
	unsigned char		alt_h;
	unsigned char		alt_m;
	unsigned char		alt_l;
	unsigned short		speed;
	unsigned short		direction;
	unsigned long		odometer;
	unsigned char		hdop;
	unsigned char		sat_num;
	unsigned char		input;
	unsigned char		output;
	unsigned char		veh_status;
	unsigned short		analog_in1;
	unsigned short		analog_in2;
	unsigned char		rtc_hour;
	unsigned char		rtc_min;
	unsigned char		rtc_sec;
	unsigned char		rtc_year;
	unsigned char		rtc_month;
	unsigned char		rtc_day;
	unsigned char		send_hour;
	unsigned char		send_min;
	unsigned char		send_sec;
	unsigned char		send_year;
	unsigned char		send_month;
	unsigned char		send_day;
}MSG_OLD_POSITION;

/* Message Format- HeartBeat */
typedef struct _MSG_OLD_HB {
	MSG_OLD_HEADER		msg_header;
	unsigned long long	modem_id;
	unsigned short		msg_id;
	var16				data_len;
	unsigned char		rtc_hour;
	unsigned char		rtc_min;
	unsigned char		rtc_sec;
	unsigned char		rtc_year;
	unsigned char		rtc_month;
	unsigned char		rtc_day;
}MSG_OLD_HB;

#define MSG_OLD_HB_LEN			sizeof(MSG_OLD_HB)
#define MSG_OLD_POSITION_LEN	sizeof(MSG_OLD_POSITION)

/**************************************************************/
typedef enum _ACK_STATE {
	WAIT_ACK,
	ACK_SUCCESS,
	ACK_ERROR,
}ACK_STATE;

extern void Msg_Init(void);
extern void Msg_Input(void);
extern void Msg_Output(void);
extern void Msg_Write(unsigned char port, char *buf, unsigned char msg_type);
extern void Msg_Res_Write(unsigned char port, char *buf);
extern void Log_Write(Msg_Recode rec, unsigned short id);
extern void Msg_Send_HB(void);
extern void Pack_Log_Data(unsigned short id, LOG_MSG *pbuf);
ACK_STATE Get_Ack_State(void);
extern void DBG_Msg(char *buf);
void RePack_Log_Time_Data(LOG_MSG *pbuf); //joseph 20101104 add

//extern void SENSOR_TEST_Msg(char *buf);     //fenris 20101206 for test Gsensor

extern unsigned char msec_count;
extern unsigned char angle_mcnt;
extern unsigned char pressure_cnt;  //fenris 20101207 


extern char gDbg_Gsm;


#ifdef __cplusplus
}
#endif	/* __cplusplus */
#endif	/* __MESSAGE_H_ */